﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Transactions;

using Bids.Core.DomainObjects;
using Bids.Data;
using Bids.Service;

namespace Bids.Web.Controllers
{
    [Authorize]
    public class EmployerController : Controller
    {
        IRepository<Task> _iRpoTask;
        IUserContext _iUserContext;
        IRepository<Contribution> _iRpoContr;
        IRepository<Score> _iRpoScore;
        ISystemService _iSysSrv;
        public EmployerController(IRepository<Task> iRpoTask,
            IUserContext iUserContext,
            IRepository<Contribution> iRpoContr,
            IRepository<Score> iRpoScore,
            ISystemService iSysSrv)
        {
            _iRpoTask = iRpoTask;
            _iUserContext = iUserContext;
            _iRpoContr = iRpoContr;
            _iRpoScore = iRpoScore;
            _iSysSrv = iSysSrv;
        }
        public ActionResult Index()
        {

            return View(_iRpoTask.Where(t => t.EmployerId == _iUserContext.User.ID).OrderByDescending(t=>t.CreateTime));
        }
        public ActionResult TaskInfo(int ID,int? page)
        {
            var task = _iRpoTask.Find(ID);
            TempData["page"] = 1;
            if (page.HasValue)
            {
                TempData["page"] = page.Value ;
                return PartialView("_WorksList", task);
            }
            return View(task);
        }

        [HttpPost]
        public ActionResult Comment(int id)
        {
            return View();
        }
        [HttpPost]
        public string SelectEnter(int id)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    var contr = _iRpoContr.Find(id);
                    contr.Task.DesignerId = contr.DesignerId;
                    contr.Status = (int)Bids.Core.DomainObjects.ContributionStatus.修稿中;
                    if (contr.ContributionItems.Count == 1)
                    {
                        var score = new Score
                        {
                            UserId = contr.DesignerId,
                            CreateTime = DateTime.Now,
                            ScoreValue = _iSysSrv.Sys.FinalistScore,
                            Explain = "项目" + contr.Task.Name + "入围,系统评分"
                        };
                        _iRpoScore.Add(score);
                    }
                    _iRpoContr.Update(contr);
                    ts.Complete();
                }
                return "succeed";
            }
            catch
            {
                return "failed";
            }
        }
        [HttpPost]
        public string ModifyContr(int id,int winning,string message)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    var contr = _iRpoContr.Find(id);
                    if (!string.IsNullOrEmpty(message))
                    {
                        contr.Comments.Add(new Comment
                        {
                            Content = message,
                            CreateTime = DateTime.Now,
                            ContributionId = contr.ID,
                            UserId = _iUserContext.User.ID
                        });
                    }
                    if (winning == 1)
                    {
                        contr.Task.DesignerId = contr.DesignerId;
                        contr.Task.Status = (int)TaskStatus.已结束;
                        contr.Status = (int)Bids.Core.DomainObjects.ContributionStatus.已截稿;
                    }
                    _iRpoContr.Update(contr);
                    ts.Complete();
                }
                return "succeed";
            }
            catch
            {
                return "failed";
            }
        }
        [HttpPost]
        public string Rank(int id, int rankscore,string explain)
        {
            
                var contr = _iRpoContr.Find(id);
                if (contr.Task.EmployerId == _iUserContext.User.ID)
                {
                    try
                    {
                        contr.RankScore = rankscore;
                        contr.RankTime = DateTime.Now;
                        contr.RankExplain = explain;
                        var score = new Score
                        {
                            UserId = contr.DesignerId,
                            CreateTime = DateTime.Now,
                            ScoreValue = rankscore,
                            Explain = "项目" + contr.Task.Name + "结束,雇主评分"
                        };

                        using (TransactionScope ts = new TransactionScope())
                        {
                            _iRpoContr.Update(contr);
                            _iRpoScore.Add(score);
                            ts.Complete();
                            return "succeed";
                        }
                    }
                    catch (Exception e)
                    {
                        return e.Message;
                    }
                }
                
            return "这个任务不属于您,您不能评价";
        }
        [HttpPost]
        public string PostDiscus(int id, string message)
        {
            try
            {
                var task = _iRpoTask.Find(id);
                task.Discussionss.Add(new Discussion
                {
                    UserId = _iUserContext.User.ID,
                    Content = message,
                    CreateTime = DateTime.Now,
                    TaskId = task.ID
                });
                _iRpoTask.Update(task);
                return "succeed";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
}
